import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiExcludeController, ApiOperation, ApiTags } from '@nestjs/swagger';
import { TenantBindDto, TenantCreateDto } from './tenant.dto';
import { AuthUser } from '../auth/decorators/auth-user.decorator';
import { TenantService } from './tenant.service';
import { TenantEntity } from './tenant.entity';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { Pagination } from '~/helper/paginate/pagination';
import { FindTenantListDto } from './tenant.dto';

@ApiTags(ModuleSwaggerTags.TENANT)
@ApiBearerAuth()
@Controller('cms/tenant')
export class TenantController {
  constructor(private readonly tenantService: TenantService) {}

  @Get()
  @ApiOperation({ summary: '获取租户列表' })
  async list(@Query('name') name: string): Promise<TenantEntity[]> {
    return await this.tenantService.getAllTenants(name);
  }

  @Get('all')
  @ApiOperation({ summary: '获取所有租户列表（包括已删除的）' })
  async listAll(): Promise<TenantEntity[]> {
    return await this.tenantService.getAllTenantsIncludeDeleted();
  }

  @Get(':id')
  @ApiOperation({ summary: '获取当前租户' })
  async tenant(@Param('id') id: number): Promise<TenantEntity> {
    return await this.tenantService.getTenantById(id);
  }

  @Post()
  @ApiOperation({ summary: '创建租户' })
  // @Perm(permissions.CREATE)
  @ApiBody({ type: TenantCreateDto })
  async create(@Body() dto: TenantCreateDto) {
    return await this.tenantService.createTenant(dto);
  }

  @Put(':id')
  @ApiOperation({ summary: '保存租户信息' })
  @ApiBody({ type: TenantCreateDto })
  async update(@Param('id') id: number, @Body() dto: TenantCreateDto) {
    return await this.tenantService.updateTenant(id, dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除租户' })
  async del(@Param('id') id: number, @AuthUser() user: IAuthUser) {
    return await this.tenantService.deleteTenant(id);
  }

  @Post(':id/restore')
  @ApiOperation({ summary: '恢复租户' })
  async restore(@Param('id') id: number, @AuthUser() user: IAuthUser) {
    return await this.tenantService.restoreTenant(id);
  }

	@Post('bind')
	@ApiOperation({ summary: '绑定租户' })
	@ApiBody({ type: TenantBindDto })
	async bind(@Body() dto: TenantBindDto): Promise<void> {
		await this.tenantService.bindTenant(dto.userId, dto.tenantId);
	}

	@Get('list')
	@ApiOperation({ summary: '获取当前用户的租户列表' })
	async getUserTenants(@Query() dto: FindTenantListDto): Promise<Pagination<TenantEntity>> {
		return this.tenantService.findTenantsByList(dto)
	}

  // TODO 修改租户信息
  // TODO 修改租户状体
  // TODO 修改租户到期时间
}
